package com.basaji.security;

import java.util.Date;

import org.acegisecurity.Authentication;
import org.acegisecurity.AuthenticationException;
import org.acegisecurity.BadCredentialsException;
import org.acegisecurity.providers.AuthenticationProvider;

import com.basaji.domain.pojo.Account;
import com.basaji.manager.IUserManager;


/**
 * process the {@link PrincipalPasswordAuthenticationToken}.
 */
public class LoginAuthenticationProvider implements AuthenticationProvider {

//    private PasswordEncoder passwordEncoder;

    //private LoginProvider[] loginProviders = new LoginProvider[0];
    
    private IUserManager userManager;

    /**
     * {@inheritDoc}
     */
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        if (authentication.getPrincipal() == null) {
            throw new BadCredentialsException("null principal");
        }
        if (authentication.getCredentials() == null) {
            throw new BadCredentialsException("Bad credentials");
        }
        Account account = retrieveLogin((PrincipalPasswordAuthenticationToken) authentication);
        if (account == null) {
            throw new BadCredentialsException("cannot find the login object");
        }
        if(!account.getPassword().equals(authentication.getCredentials().toString())){  //Util.MD5(
        	throw new BadCredentialsException("Bad credentials");
        }
        account.setLastLogin(new Date());
        account.setLoginTimes(account.getLoginTimes()+1);
        userManager.update(account);
        return UulogSecurityContext.createSuccessAuthentication(account,
                (PrincipalPasswordAuthenticationToken) authentication);
    }

    private Account retrieveLogin(PrincipalPasswordAuthenticationToken authentication){
       Object principal = authentication.getPrincipal();
       try{
    	   Account _account = userManager.getAccountByUsername(principal.toString()); 
    	   return _account;
       }catch(Exception e){
    	   return null;
    	   //throw new BadCredentialsException("cannot find the login object");
       }
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public boolean supports(Class clazz) {
        return (PrincipalPasswordAuthenticationToken.class.isAssignableFrom(clazz));
    }

	public IUserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(IUserManager userManager) {
		this.userManager = userManager;
	}

    /**
     * set an array of login providers used by this class.
     * 
     * @param loginProviders
     *            an array of login providers.
     */
//    @Required
//    public void setLoginProviders(LoginProvider[] loginProviders) {
//        Assert.notNull(loginProviders, "should not set login providers to null");
//        for (int i = 0; i < loginProviders.length; i++) {
//            Assert.notNull(loginProviders[i], "there is a null login provider in the list.");
//        }
//        this.loginProviders = new LoginProvider[loginProviders.length];
//        System.arraycopy(loginProviders, 0, this.loginProviders, 0, this.loginProviders.length);
//    }

}
